package com.example.zhc36.keymanager;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.zhc36.keymanager.Daopwd.Dao;
import com.example.zhc36.keymanager.adapter.Adapter;
import com.example.zhc36.keymanager.bean.Daomoli;

import java.util.List;

import static com.example.zhc36.keymanager.R.*;
import static com.example.zhc36.keymanager.R.id.*;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private  Dao mo;
     List<Daomoli> list;

    private ListView lv;
    private Handler hand;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        lv=findViewById(id.lv);
//         lv.setOnClickListener(this);
  lv.setOnItemClickListener(this);
         mo= new Dao(this);
         list=mo.getall(null);
        Adapter adapter=new Adapter(list);

        lv.setAdapter(adapter);




    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        adapterView.getAdapter().getItem(i);
        View v=   view.findViewById(R.id.tv1);

        hand = new Handler();
        alertDialog = new AlertDialog.Builder(this)
                .setTitle("请稍后。。。")
                .setCancelable(false)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "开门任务被终止", Toast.LENGTH_SHORT).show();
                        hand.removeCallbacksAndMessages(null);


                    }
                })
                .create();
        alertDialog.setMessage("正在开门"+list.get(i).getRoom());
        alertDialog .show();
        hand.postDelayed(new Runnable() {
            @Override
            public void run() {
                alertDialog.dismiss();
                Toast.makeText(MainActivity.this, "开门成功", Toast.LENGTH_SHORT).show();

            }
        },2000);
    }

    @Override
    protected void onDestroy() {
        hand.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}
